package model.humanbody

import groovy.transform.CompileStatic
import model.agent.AgentVariable
import model.agent.question.QuestionSet;

import org.apache.poi.hssf.usermodel.HSSFCell
import org.apache.poi.hssf.usermodel.HSSFRow
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.Row


class BodyBuilder {

	//	public static HumanBody build_old(String path){
	//
	//		if(!(new File(path)).exists()){
	//			println "文件${path}不存在"
	//			throw new FileNotFoundException("文件\"${path}\"不存在")
	//		}
	//
	//		String ext = path.split('\\.').last()
	//
	//		HSSFSheet sheet
	//		HSSFRow row
	//		HSSFCell cell
	//
	//		Iterator<Row> rowIterator
	//
	//		FileInputStream file = new FileInputStream(new File(path))
	//
	//		HSSFWorkbook wb = new HSSFWorkbook(file)
	//
	//		HumanBody body = new HumanBody()
	//
	//		//======添加脏器和脏器内部的变量======
	//		sheet = wb.getSheet("局部变量")
	//
	//		rowIterator = sheet.iterator();
	//
	//		row = (HSSFRow)rowIterator.next() //跳过第一行
	//
	//		String oName
	//		String lastName
	//
	//		Organ organ
	//
	//		while (rowIterator.hasNext()){
	//			row = (HSSFRow)rowIterator.next()
	//
	//			cell = row.getCell(1)
	//
	//			//如果这一行没东西，则说明还是属于最后一个organ
	//			if (cell.toString().trim() != ""){
	//				oName = cell.toString()
	//				lastName = oName
	//				organ = new Organ(oName)
	//				body.addOrgan(organ)
	//			}else{
	//				oName = lastName
	//			}
	//
	//			String pName = row.getCell(5).toString().trim()
	//
	//			if(pName!=""){
	//				def op = new AgentVariable(pName)
	//				organ.addVariable(op)
	//			}
	//
	//		}// while
	//
	//		//========脏器和脏器内部变量结束========
	//
	//		//========添加整体变量（主体body的变量）==========
	//		sheet = wb.getSheet("整体变量")
	//
	//		rowIterator = sheet.iterator();
	//
	//		row = (HSSFRow)rowIterator.next() //跳过第一行
	//
	//		while (rowIterator.hasNext()){
	//			row = (HSSFRow)rowIterator.next()
	//
	//			cell = row.getCell(6)
	//
	//			String pName = cell.toString().trim()
	//
	//			if(pName!=""){
	//				def op = new AgentVariable(pName)
	//				body.addVariable(op)
	//			}
	//
	//		}// while
	//
	//		//=====整体变量结束==============
	//
	//		//添加外界影响因素，作为整体变量来添加=========================
	//		//判断依据是HumanBody.searchvariable()找不到
	//
	//		sheet = wb.getSheet("局部变量")
	//
	//		rowIterator = sheet.iterator();
	//
	//		row = (HSSFRow)rowIterator.next() //跳过第一行
	//
	//		while (rowIterator.hasNext()){
	//			row = (HSSFRow)rowIterator.next()
	//			cell = row.getCell(3)
	//			String pName = cell.toString().trim()
	//
	//			if(pName!="" && body.searchVariableOnAgent(pName)==null){
	//				def op = new AgentVariable(pName)
	//				body.addVariable(op)
	//			}
	//		}
	//
	//		sheet = wb.getSheet("整体变量")
	//
	//		rowIterator = sheet.iterator();
	//
	//		row = (HSSFRow)rowIterator.next() //跳过第一行
	//
	//		while (rowIterator.hasNext()){
	//			row = (HSSFRow)rowIterator.next()
	//			cell = row.getCell(1)
	//			String pName = cell.toString().trim()
	//
	//			if(pName!="" && body.searchVariableOnAgent(pName)== null){
	//				def op = new AgentVariable(pName)
	//				body.addVariable(op)
	//			}
	//		}
	//
	//		return body
	//	}

	public static HumanBody build(String path,String agentSheet, String affectionSheet){

		if(!(new File(path)).exists()){
			println "文件${path}不存在"
			throw new FileNotFoundException("文件\"${path}\"不存在")
		}

		String ext = path.split('\\.').last()

		HSSFSheet sheet
		HSSFRow row
		HSSFCell cell

		Iterator<Row> rowIterator

		FileInputStream file = new FileInputStream(new File(path))

		HSSFWorkbook wb = new HSSFWorkbook(file)

		HumanBody body = new HumanBody()

		//======添加脏器和脏器内部的变量======
		sheet = wb.getSheet(agentSheet)

		rowIterator = sheet.iterator();

		row = (HSSFRow)rowIterator.next() //跳过第一行

		String oName
		String lastName

		Organ organ

		while (rowIterator.hasNext()){
			row = (HSSFRow)rowIterator.next()

			oName = row.getCell(0).toString().trim()

			//如果这一行没东西，则说明还是属于最后一个organ
			if (oName != ""){
				if(!body.subAgents.containsKey(oName)){
					organ = new Organ(oName)
					body.addOrgan(organ)
				}
				organ = body.subAgents[oName]

			}

			String pName = row.getCell(1).toString().trim()

			if(pName != ""){
				def var = new AgentVariable(pName)
				organ.addVariable(var)

				String eQuestion = row.getCell(5).toString().trim()

				String lQuestion = row.getCell(6).toString().trim()

				if(eQuestion!=""){
					def eqs = new QuestionSet(eQuestion)
					eqs.name = var.name+"确定性诊断"
					var.existentialQuestions = eqs
				}

				if(lQuestion!=""){
					def lqs = new QuestionSet(lQuestion)
					lqs.name = var.name+"程度性诊断"
					var.levelQuestions = lqs
				}
			}


		}// while

		//========脏器和脏器内部变量结束========

		//========添加影响（主体body的变量）==========
		sheet = wb.getSheet(affectionSheet)

		rowIterator = sheet.iterator();

		row = (HSSFRow)rowIterator.next() //跳过第一行

		while (rowIterator.hasNext()){
			row = (HSSFRow)rowIterator.next()

			String pName = row.getCell(0).toString().trim()

			if(pName != ""){
				def op = new AgentVariable(pName)
				body.addVariable(op)
			}

		}// while

		//=====整体变量结束==============

		return body
	}

}

